package com.atguigu.gmall.common.util;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.user.vo.UserAuthInfoVo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lfy
 * @Description 用户信息 工具类
 * @create 2022-10-17 14:47
 */
public class UserAuthUtils {

    public static UserAuthInfoVo getUserAuthInfo(){
        //1、从同一个线程绑定中拿到老请求
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //如果远程调用被放到异步线程中，就会丢失老请求

        if(attributes != null){
            HttpServletRequest request = attributes.getRequest();

            //提取出老请求中的用户标识
            String userId = request.getHeader(RedisConst.USER_ID_HEADER);
            String userTempId = request.getHeader(RedisConst.USER_TEMP_ID_HEADER);
            Long uid = null;
            try {
                uid = Long.parseLong(userId);
            }catch (Exception e){

            }


            return  new UserAuthInfoVo(uid,userTempId);
        }

        return null;
    }
}
